package com.menu.Service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.menu.Mapper.MenuFatherMapper;
import com.menu.Mapper.MenuMapper;
import com.menu.Mapper.UserMapper;
import com.menu.Pojo.Menu;
import com.menu.Pojo.MenuFather;
import com.menu.Pojo.User;
import com.menu.Service.MenuFatherService;
import com.menu.Service.NoticeService;
import com.menu.Utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MenuFatherServiceImpl implements MenuFatherService {


    @Resource
    MenuFatherMapper menuFatherMapper;
    @Resource
    MenuMapper menuMapper;
    @Resource
    UserMapper userMapper;
    @Autowired
    FileUtils fileUtils;
    @Resource
    NoticeService noticeService;
    @Resource
    RedisUtil redisUtil;

    @Override
    public ResponseResult addMenuFather(String menuFatherName, String menuFatherIntroduce,
                                        Integer[] menuIds, Integer userId, MultipartFile menuFatherSrc) {
        User user= userMapper.findByUserId(userId);
        if(user==null){
            return ResponseResult.error("用户不存在");
        }
        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date1=simpleDateFormat.format(date);
        MenuFather menuFather=new MenuFather();
        menuFather.setMenuFatherName(menuFatherName);
        menuFather.setMenuFatherIntroduce(menuFatherIntroduce);
        menuFather.setMenuFatherReleaseTime(date1);
        menuFather.setUserId(userId);
        ArrayList<Integer> arrayList=new ArrayList<>();
        for(int x:menuIds){
            Menu menu=menuMapper.findByMenuId(x);
            if(!arrayList.contains(x)&&menu!=null&&menu.getState()==1){
                arrayList.add(x);
            }else{
                return ResponseResult.error("不存在该食谱或已经添加过该食谱或该食谱未通过审核");
            }
        }
        menuFather.setMenuIds(arrayList.toString());
        menuFatherMapper.addMenuFather(menuFather);
        ResponseResult responseResult=fileUtils.uploadMenuFather(menuFatherSrc,menuFather.getMenuFatherId());
        if(responseResult.getCode()==200){
            Object object=responseResult.getObject();
            String fileName=(String)object;
            menuFatherMapper.updateMenuFatherSrc(fileName,menuFather.getMenuFatherId());
        } else{
          return responseResult;
        }
        return ResponseResult.success("添加成功");
    }

    @Override
    public ResponseResult findAllMenuFather(Integer pageNow,Integer pageSize) {
        PageHelper.startPage(pageNow,pageSize);
        List<MenuFather> menuList= menuFatherMapper.findAllMenuFather();
        PageInfo<MenuFather> pageInfo=new PageInfo<>(menuList);
        return ResponseResult.success("查询成功",pageInfo);
    }

    @Override
    public ResponseResult findMenuByMenuFatherMenuIds(Integer menuFatherId) {
        MenuFather menuFather=menuFatherMapper.findByMenuFatherId(menuFatherId);
        if (menuFather == null) {
            return ResponseResult.error("不存在该菜单");
        }
        String menuIds= menuFather.getMenuIds();
        if(menuIds==null||menuIds.equals("")){
            return ResponseResult.success("菜单下没有食谱","",menuFather);
        }
        String []o=CommonUtil.split(menuIds);
        ArrayList<Menu> arrayList=new ArrayList<>();
        if(!o[0].equals("")){
            Integer[] integers=  CommonUtil.StringToInt(o);
            for(int menuId:integers){
                Menu menu=menuMapper.findByMenuId(menuId);
                if(menu!=null&&menu.getState()==1){
                    arrayList.add(menu);
                }
            }
        }
        return ResponseResult.success("遍历菜单下食谱成功",arrayList,menuFather);
    }

    @Override
    public ResponseResult findAllNearTimeMenuFather(Integer pageNow,Integer pageSize,Integer day) {
        PageHelper.startPage(pageNow,pageSize);
        List<MenuFather> menuList= menuFatherMapper.findAllNearTimeMenuFather(day);
        PageInfo<MenuFather> pageInfo=new PageInfo<>(menuList);
        return ResponseResult.success("查询成功",pageInfo);
    }

    @Override
    public ResponseResult batchDeleteMenuFather(Integer[] menuFatherId) {
        menuFatherMapper.batchDeleteMenuFather(menuFatherId);
        return ResponseResult.success("删除成功");
    }

    @Override
    public ResponseResult deleteMenuFather(Integer menuFatherId,Integer userId) {
        User user= userMapper.findByUserId(userId);
        if(user==null){
            return ResponseResult.error("用户不存在");
        }
        MenuFather menuFather=menuFatherMapper.findByMenuFatherId(menuFatherId);
        if(menuFather!=null&&userId.equals(menuFather.getUserId())){
            menuFatherMapper.deleteMenuFather(menuFatherId);
            return ResponseResult.success("删除成功");
        }else{
            return ResponseResult.error("不存在该菜单或用户只能删除自己的菜单");
        }
    }

    @Override
    public ResponseResult batchDeleteMenuFather(Integer[] menuFatherId, Integer userId) {
        menuFatherMapper.batchDeleteMenuFather(menuFatherId);
        return ResponseResult.success("删除成功");
    }

    @Override
    public ResponseResult findByMenuFatherId(Integer menuFatherId) {
        MenuFather menuFather=menuFatherMapper.findByMenuFatherId(menuFatherId);
        if(menuFather==null){
            return ResponseResult.error("不存在该菜单");
        }
        return ResponseResult.success("根据id查找如下",menuFather);
    }

    @Override
    public ResponseResult findMenuFatherByNameLike(String key, Integer pageNow, Integer pageSize) {
        PageHelper.startPage(pageNow,pageSize);
        List<MenuFather> menuList= menuFatherMapper.findByMenuFatherNameLike(key);
        PageInfo<MenuFather> pageInfo=new PageInfo<>(menuList);
        boolean result = redisUtil.hasKey(RedisKeyUtils.ALL_IN_SEARCH);
        if (!result){
            redisUtil.addZset(RedisKeyUtils.ALL_IN_SEARCH,key,1);
        }else{
            redisUtil.addScore(RedisKeyUtils.ALL_IN_SEARCH,key,1);
        }
        return ResponseResult.success("查询成功",pageInfo);
    }

    @Override
    public ResponseResult findByMenuFatherByDoh(Integer pageNow, Integer pageSize) {
        PageHelper.startPage(pageNow,pageSize);
        List<MenuFather> menuList= menuFatherMapper.findByMenuFatherByDoh();
        PageInfo<MenuFather> pageInfo=new PageInfo<>(menuList);
        return ResponseResult.success("查询成功",pageInfo);
    }

    @Override
    public ResponseResult findMenuFatherByUserId(Integer userId, Integer pageNow, Integer pageSize) {
        PageHelper.startPage(pageNow,pageSize);
        List<MenuFather> menuList= menuFatherMapper.findMenuFatherByUserId(userId);
        PageInfo<MenuFather> pageInfo=new PageInfo<>(menuList);
        return ResponseResult.success("查询成功",pageInfo);
    }

    @Override
    public ResponseResult verifyMenuFather(Integer menuFatherId,Integer state) {
        MenuFather menu= menuFatherMapper.findByMenuFatherId(menuFatherId);
        if(menu==null){
            return ResponseResult.error("菜单不存在");
        }
        if(state==1){
            menuFatherMapper.updateStateToOne(menuFatherId);
            noticeService.addNotice("您的菜单"+menu.getMenuFatherName()+"审核已经通过",menu.getUserId());
            return ResponseResult.success("该菜单审核已通过");
        }
        if(state==-1){
            if(menu.getState()==0){
                menuFatherMapper.updateStateToZero(menuFatherId);
                noticeService.addNotice("您的菜单"+menu.getMenuFatherName()+"审核未通过,如果有什么疑问可以反馈哦~",menu.getUserId());
            }
            if(menu.getState()!=0){
                menuFatherMapper.updateStateToZero(menuFatherId);
                noticeService.addNotice("您的菜单"+menu.getMenuFatherName()+"经过再次审核发现违规,审核未通过,如果有什么疑问可以反馈哦~",menu.getUserId());
            }
            return ResponseResult.success("该菜单审核未通过");
        }
        return ResponseResult.error("state只能是-1或者1");
    }

    @Override
    public ResponseResult adminFindMenuFatherByNameLike(String key, Integer pageNow, Integer pageSize) {
        PageHelper.startPage(pageNow,pageSize);
        List<MenuFather> menuList= menuFatherMapper.adminFindByMenuFatherNameLike(key);
        PageInfo<MenuFather> pageInfo=new PageInfo<>(menuList);
        return ResponseResult.success("查询成功",pageInfo);
    }

    @Override
    public ResponseResult addMenuAtMenuFather(Integer userId, Integer menuId, Integer menuFatherId) {
        User user= userMapper.findByUserId(userId);
        if(user==null){
            return ResponseResult.error("用户不存在");
        }
        Menu menu= menuMapper.findByMenuId(menuId);
        if(menu==null){
            return ResponseResult.error("食谱不存在");
        }
        MenuFather menuFather= menuFatherMapper.findByMenuFatherId(menuFatherId);
        if(menuFather==null){
            return ResponseResult.error("菜单不存在");
        }
        if(!menuFather.getUserId().equals(userId)){
            return ResponseResult.error("你不是该菜单的创建人,不能在该菜单中添加食谱");
        }
        ArrayList<String> menuList;
        menuList= CommonUtil.splitAdd(menuFather.getMenuIds());
        if(menuList.contains(String.valueOf(menuId))){
            return ResponseResult.error("菜单中已经存在该食谱id: "+menuId);
        }
        menuList.add(String.valueOf(menuId));
        menuFatherMapper.updateMenuIds(menuFatherId,menuList.toString());
        MenuFather menuFather1= menuFatherMapper.findByMenuFatherId(menuFatherId);
        return ResponseResult.success("添加菜谱成功",menuFather1);
    }

}
